$(() => {
    // 获取area_id
    const token = GetQueryString("token");
    let area_id = GetQueryString('area_id');

    $.ajax({
        type: "post",
        url: url,
        data: { 'method': "Plat|water_index" },
        dataType: 'json',
        success: function(res) {
            // console.log(res);
            if (res.status == 1) {
                $('.hetitle').html(res.title);
                // 分类
                let pages = [];
                res.category_list.forEach((item, index) => {
                    let page = Math.floor(index / 10);
                    // 如果当前下标无值，返空数组
                    if (!pages[page]) {
                        pages[page] = [];
                    }
                    pages[page].push(item);
                });
                // // console.log(pages);
                let wrapper = $('.swiper-wrapper');
                pages.forEach((page, idx) => {
                    // // console.log(page);
                    wrapper.append('<div class="swiper-slide"></div>');
                    let slide = $('.swiper-slide');
                    page.forEach(item => {
                        // // console.log(item)
                        slide.eq(idx).append(`<a href="companyLists.html?area_id=${item.area_id}&id=${item.id}&cat_name=${item.bname}" class="izlink classify_link"><img src="${item.logo_url}" alt="${item.bname}" class="izpic" /><p class="cat_name">${item.bname}</p></a>`);
                    })
                })
                //滑动导航
                new Swiper('.swiper-container', {
                    pagination: '.swiper-pagination'
                })

                // 品牌
                let com = '';
                res.company_list.forEach(item => {
                    if (item.company_level >= 1) {
                        com += `
                    <a href="shop_details_company.html?company_id=${item.id}&token=${token}" class="inlist1link">`
                    } else {
                        com += `
                    <a href="shop_details.html?company_id=${item.id}&token=${token}" class="inlist1link">`
                    }
                    com += `<img src="${item.company_poster_thumb}" alt="${item.company_name}" class="inlistpic">
                    </a>
                   `;
                })
                $('.waterBrandLists').html(com);

                // 推荐商品
                let goods = '';
                res.goods_list.forEach(item => {
                    goods += `
                    <li class="iz2item">
                        <a href="comp_shopDetail.html?id=${item.id}&token=${token}" class="iz2link">
                            <img src="${item.goods_thumb == '' ? '../img/banner.png' : item.goods_thumb}" alt="${item.goods_name}" class="iz2pic" />
                            <div class="iz2title">
                                <p class="iz2ti1">${item.description}</p>`
                    if (item.price_show_model == 1) {
                        goods += `<p class="iz2ti2">¥${item.goods_price}</p>`
                    } else if (item.price_show_model == 2) {
                        goods += `<p class="iz2ti2"><del style="color:#666;font-size:.24rem;">¥${item.price_before}</del>/<span style="font-size:.28rem;">¥${item.goods_price}</span></p>`
                    } else {
                        goods += `<p class="iz2ti2">${item.price_area}</p>`
                    }
                    goods += `</div>
                        </a>
                    </li>
                    `;
                })
                $('.moreGoodsLists').html(goods);
            } else if (res.status == 2) {
                location.href = "fox://logout";
            } else {
                alert(res.message);
            }
        }
    })

    $('.search').on('click', function() {
        location.href = "search.html?area_id=2&token=" + token;
    })

    // 更多品牌跳转
    $(document).on('click', '.moreBrand', function() {
        location.href = 'more_brand.html?area_id=' + area_id + '&recommend_position=2' + '&token=' + token;
    })
    // 更多推荐商品
    $(document).on('click', '.more_shopLists', function() {
        location.href = 'more_goods_A.html?area_id=' + area_id + '&recommend_position=2' + '&token=' + token;
    });
})